// 以下JS函数用于获取url参数
// @param variable 需获取的参数key
function getQueryVariable(variable) {
    var query = window.location.search.substring(1);
    var vars = query.split("&");
    for (var i = 0; i < vars.length; i++) {
        var pair = vars[i].split("=");
        if (pair[0] == variable) { return pair[1]; }
    }
    return (false);
}

// 校验登陆
function isValiUser() {
    $.ajax({
        type: 'GET',    //get请求无法附带data：json数据
        url: 'control/loginstatus.php',
        success: function (result) {
            if (result) {
                console.log("用户已登录");
            } else {
                console.log("用户未登录");
                window.location.href = "./";
            }
        }
    })
}

function submitResume() {
    var name = document.getElementById("name").value;
    var htmlstruct = document.getElementById("doc").innerHTML;

    if (name == "") {
        alert("请输入姓名");
    } else {
        $.ajax({
            type: 'POST',
            url: 'control/saveresume.php',
            dataType: 'json',
            data: {
                "name": name,
                "htmlstruct": htmlstruct
            },
            success: function (result) {
                if (result) {
                    alert("提交成功");
                    window.location.href = "./";
                } else {
                    alert("提交失败,请登陆");
                    window.location.href = "./";
                }
            }
        })
    }
}

function readResume(){
    var id = getQueryVariable("id");

    $.ajax({
        type: 'GET',
        url: 'control/c_readresume.php',
        dataType: 'json',
        data: {
            "id": id
        },
        success: function (result) {
            
            if (result) {
                document.getElementById("name").value = result.name;
                document.getElementById("doc").innerHTML = result.htmlstruct;
            } else {
                alert("读取失败,请登陆");
                window.location.href = "./resumelist.html";
            }
        }
    })
}

function updateResume() {
    var id = getQueryVariable("id");
    var name = document.getElementById("name").value;
    var htmlstruct = document.getElementById("doc").innerHTML;

    if (name == "") {
        alert("请输入姓名");
    } else {
        $.ajax({
            type: 'POST',
            url: 'control/c_updateresume.php',
            dataType: 'json',
            data: {
                "id": id,
                "name": name,
                "htmlstruct": htmlstruct
            },
            success: function (result) {
                if (result) {
                    alert("修改成功");
                    window.location.href = "./resumelist.html";
                } else {
                    alert("修改失败,请登陆");
                    window.location.href = "./resumelist.html";
                }
            }
        })
    }
}